home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1156 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.1 KB

  1. Path: navet.enator.se!usenet
  2. From: Enator A/S <ENATOR@ENATOR.DK>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: OO design issue
  5. Date: 9 Jan 1996 17:28:14 GMT
  6. Organization: Enator A/S
  7. Message-ID: <4cu8je$kgf@navet.enator.se>
  8. References: <4ctp93$2gu@news.kth.se>
  9. NNTP-Posting-Host: 193.162.31.48
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  14.  
  15. Dear Anders,
  16.  
  17. If I got you right, your problem is very common, and the design issue is 
  18. a very important one.
  19.  
  20. Of course you can just let your member-classes have a reference to the 
  21. parent-class, but it is very bad design. You should allways try to make a 
  22. simple object-composition hiararchy, and that means:  memberobjects do 
  23. not know parents. One of the reasons is reuseability - if the member has 
  24. a reference to the parent, the member can't be used in an environment, 
  25. where the parent does not exists.
  26.  
  27. The solution is described in the 'Design Pattern' book by Erich Gamma and 
  28. others, and it is called an (plug-in)adapter. You get nothing for free, 
  29. and what you have to pay here is the cost of two new classes. 
  30.  
  31. Together with your Triangle (member) class you supply an abstract base 
  32. class TriangleAdapter and your Triangle constructor must have a pointer 
  33. to a TriangleAdapter as a parameter, and save it for later use. The 
  34. TriangleAdapter must have a pure virtual function: GetXCoordinate, which 
  35. can be called from the Triangle class. A class, which wants  to use a 
  36. Triangle object now has to derive a class from TriangleAdapter and 
  37. implement the function GetXCoordinate here. That means your parent 
  38. TrianglesAndNodes must make an instance of 
  39. a TriangelAdapterTrianglesAndNodes class which has implemented 
  40. GetXCoordinate, by calling GetXCoordinate in TrianglesAndNodes, and 
  41. handle a reference to this adapter over to the  Triangle object. 
  42.  
  43. The magic is, that by using the virtual mechanism you can invert the 
  44. direction of the relation and thereby loosen up constraints. This ability 
  45. makes the adapter pattern very important to C++ programmers.
  46.  
  47.  
  48. Hardy Henneberg 
  49. ENATOR Danmark
  50.  
  51.  
  52.